< previous page page_253 next page >

Page 253
Adding Items to the List
The next step is to write some code to add items to the ListView. You can use the sample code in Listing 12.1 to add objects to the ListItems collection. Run the program, and you should see the results pictured in Figure 12.6.
Listing 12.1 LISTVIEW.ZIPUsing the Load Event to Set Up the ListView Control
Private Sub Form_Load()

    'Set up the icons from the image List
    lvMain.Icons = ilNormal
    lvMain.SmallIcons = ilSmall
    lvMain.ColumnHeaderIcons = ilHeader

    ' Add ColumnHeaders.  The width of the columns is the width
    ' of the control divided by the number of ColumnHeader objects.
    Dim clmX As ColumnHeader
    Set clmX = lvMain.ColumnHeaders.Add(, , "Team", , , 1)
    Set clmX = lvMain.ColumnHeaders.Add(, , "Wins", , , 2)
    Set clmX = lvMain.ColumnHeaders.Add(, , "Losses", , , 3)

    'Create a ListItem object.
    Dim itmX As ListItem

    'Add some data setting to the ListItem:

    'Red Sox Stats:
    Set itmX = lvMain.ListItems.Add(, , "Red Sox", 1, 1)  ' Team
    itmX.SubItems(1) = "64"                               ' Wins
    itmX.SubItems(2) = "65"                               ' Losses

    ' You can duplicate the above 3 lines of code
    ' to add information for more teams . . .

    lvMain.View = lvwReport ' Set View property to Report.

 End Sub
0253-01.jpg
Figure12.6
The ListView looks like this
after being set up with your code.
In Listing 12.1, notice that temporary object variables are used to set up the ColumnHeaders and ListItems collections. You first call the Add method, which returns you to a reference to the object it just added. For the code that sets up the column headings, this was not really necessary because you do not refer to the temporary value clmX beyond the initial call to the Add method.

 
< previous page page_253 next page >